package kujie.mongo.controller;

import kujie.mongo.domain.mongo.QuanziPostMongoBO;
import kujie.mongo.service.IMongoService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 *
 *
 * @author check
 * @date 2025-10-18
 */
@RestController
@RequestMapping("/test")
@RequiredArgsConstructor
public class MongoController {
    private final IMongoService mongoService;

    @PostMapping("/insert")
    public Boolean insert() {
        List<QuanziPostMongoBO> posts = generateMockPosts(20);
        return mongoService.saveBatch(posts);
    }

    @GetMapping("/query")
    public QuanziPostMongoBO query() {
        QuanziPostMongoBO quanziPostBO = null;
        try {
            quanziPostBO = mongoService.lambdaQuery().eq(QuanziPostMongoBO::getPostId, 1029).one();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return quanziPostBO;
    }

    @GetMapping("/queryAll")
    public List<QuanziPostMongoBO> queryAll() {
        return mongoService.list();
    }

    private List<QuanziPostMongoBO> generateMockPosts(int count) {
        List<QuanziPostMongoBO> posts = new ArrayList<>();
        Random random = new Random();

        String[] locations = {"北京市", "上海市", "广州市", "深圳市", "杭州市", "成都市"};
        String[][] coordinates = {
                {"39.9042", "116.4074"}, // 北京
                {"31.2304", "121.4737"}, // 上海
                {"23.1291", "113.2644"}, // 广州
                {"22.5431", "114.0579"}, // 深圳
                {"30.2741", "120.1551"}, // 杭州
                {"30.5728", "104.0668"}  // 成都
        };

        String[] labels = {
                "生活,日常,分享",
                "技术,编程,Java",
                "美食,烹饪,食谱",
                "旅游,风景,摄影",
                "运动,健康,健身",
                "工作,职场,经验"
        };

        for (int i = 1; i <= count; i++) {
            QuanziPostMongoBO post = new QuanziPostMongoBO();
            int locationIndex = random.nextInt(locations.length);
            post.setPostId(i);
            post.setUserId(i);
            post.setPostType(random.nextInt(3) + 1); // 1-3随机
            post.setTitle(generateTitle(post.getPostType(), i));
            post.setContent(generateContent(post.getPostType()));
            post.setPicId(generatePicIds(random));
            post.setPostReleaseTime(LocalDateTime.now().minusDays(random.nextInt(30)));
            post.setPostLocation(locations[locationIndex]);
            post.setPostReleaseType(random.nextInt(2) + 1);
            post.setPostVisibleState(random.nextInt(3) + 1);
            post.setLatitude(coordinates[locationIndex][0]);
            post.setLongitude(coordinates[locationIndex][1]);
            post.setPostStatus(random.nextInt(4)); // 0-3随机
            post.setLabel(labels[random.nextInt(labels.length)]);
            post.setTagFriends(generateTagFriends(random));
            post.setNumOfCollections(random.nextInt(50));
            post.setNumOfComments(random.nextInt(100));
            post.setNumOfLikes(random.nextInt(200));
            post.setTenantId("tenant_" + String.format("%03d", random.nextInt(5) + 1));
            post.setCreateDept(1000L + random.nextInt(10));
            post.setCreateBy(1000L + random.nextInt(50));
            post.setCreateTime(LocalDateTime.now().minusDays(random.nextInt(30)));
            post.setUpdateBy(1000L + random.nextInt(50));
            post.setUpdateTime(LocalDateTime.now());
            post.setRemark("模拟数据 " + i);

            posts.add(post);
        }
        return posts;
    }


    private String generateTitle(int postType, int index) {
        switch (postType) {
            case 1:
                return "日常分享帖子 " + index;
            case 2:
                return "精彩活动邀约 " + index;
            case 3:
                return "资源分享 " + index;
            default:
                return "帖子标题 " + index;
        }
    }

    private String generateContent(int postType) {
        switch (postType) {
            case 1:
                return "这是一篇日常分享的帖子内容，记录生活中的点点滴滴...";
            case 2:
                return "活动详情：欢迎大家参加我们的活动，时间地点如下...";
            case 3:
                return "资源分享：这里有一些有用的资源和资料...";
            default:
                return "帖子内容...";
        }
    }

    private List<Integer> generatePicIds(Random random) {
        List<Integer> picIds = new ArrayList<>();
        int count = random.nextInt(4) + 1; // 1-4张图片
        for (int i = 0; i < count; i++) {
            picIds.add(5000 + random.nextInt(1000));
        }
        return picIds;
    }

    private List<Integer> generateTagFriends(Random random) {
        List<Integer> friends = new ArrayList<>();
        int count = random.nextInt(4); // 0-3个好友
        for (int i = 0; i < count; i++) {
            friends.add(1000 + random.nextInt(50));
        }
        return friends;
    }
}
